<?php
abstract class Controller {
	protected $id;
	protected $template;
	protected $children = array();
	protected $data = array();
	protected $output;

	public function __get($key) {
		return Registry::get($key);
	}

	public function __set($key, $value) {
		Registry::set($key, $value);
	}

	protected function forward($route, $args = array()) {
		return new Router($route, $args);
	}

	protected function redirect($url) {
		header('Location: ' . str_replace('&amp;', '&', $url));
		exit();
	}

	protected function render($return = FALSE) {
		foreach ($this->children as $child) {
			$file = DIR_APPLICATION . 'controller/' . str_replace('../', '', $child) . '.php';
			$class = 'Controller' . preg_replace('/[^a-zA-Z0-9]/', '', $child);

			if (file_exists($file)) {
				require_once($file);

				$controller = new $class();

				$controller->index();

				$this->data[$controller->id] = $controller->output;
			} else {
				exit('Error: Could not load controller ' . $child . '!');
			}
		}

		if ($return) {
			return $this->fetch($this->template);
		} else {
			$this->output = $this->fetch($this->template);
		}
	}

	protected function fetch($filename) {
		$file = DIR_TEMPLATE . $filename;

		if (file_exists($file)) {
			extract($this->data);

			ob_start();

			require($file);

			$content = ob_get_contents();

			ob_end_clean();

			return $content;
		} else {
			exit('Error: Could not load template ' . $file . '!');
		}
	}

	//d ppl ISSUE 4
	protected function glob($pattern, $flags = null) {
		return $this->safe_glob($pattern,$flags);
	}
	function safe_glob($pattern, $flags=0) {
		$split=explode('/',$pattern);
		$match=array_pop($split);
		$path=implode('/',$split);
		if (($dir=opendir($path))!==false) {
			$glob=array();
			while(($file=readdir($dir))!==false) {
				if (fnmatch($match,$file)) {
					if ((is_dir("$path/$file"))||(!($flags&GLOB_ONLYDIR))) {
						if ($flags&GLOB_MARK) $file.='/';
						$glob[]=$file;
					}
				}
			}
			closedir($dir);
			if (!($flags&GLOB_NOSORT)) sort($glob);
			return $glob;
		} else {
			return false;
		}
	}
		//f ppl
}
?>